package com.xyy.saas.payment.merchant.core.dto;

import com.xyy.saas.payment.common.enums.AmountTypeEnum;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.FbankTradeCodeEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.model.SeparateOrder;
import com.xyy.saas.payment.util.DateUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

@Data
@Builder
public class AccountTransferRefundDto {
    /**
     * 转账类型
     * 6 退款
     */
    private String transferType;
    /**
     * 业务线
     */
    private String businessIdType;
    /**
     * 支付平台商户标识
     */
    private String origOutAccountId;
    /**
     * 支付平台商户标识
     */
    private String origInAccountId;
    /**
     * 金额
     */
    private Integer amount;
    /**
     * 手续费
     */
    private Integer fee;
    /**
     * 交易单号
     */
    private String origTransferNo;

    private String origOrderNo;

    /**
     * 业务单号
     */
    private String businessOrderNo;
    /**
     * 逆向交易单号
     */
    private String refundTransferNo;
    /**
     * 业务逆向单号
     */
    private String businessRefundNo;
    /**
     * 备注
     */
    private String remark;
    /**
     * 扩展字段
     * 日期格式：20220926
     */
    private String extend;

    /**
     * 重写get方法
     */
    public String getTransferType() {
        return StringUtils.isBlank(transferType) ? "2" : transferType;
    }

    /**
     * 重写get方法
     */
    public Integer getFee() {
        return fee == null ? NumberUtils.INTEGER_ZERO : fee;
    }

    /**
     * 参数校验
     */
    public void checkParam() throws PaymentException {
        if(StringUtils.isBlank(businessIdType)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务线不能为空");
        }

        if(amount == null || amount <= 0) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "转账退款金额必须大于0");
        }

        if(StringUtils.isBlank(businessOrderNo)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务单号不能为空");
        }
    }

    private void fillSeparateOrderBizNo(SeparateOrder separateOrder) {
        String bizOrderNo = separateOrder.getBusinessOrderNo();
        if (bizOrderNo.contains(AmountTypeEnum.PINGAN_CREDIT_PAY_ACCT_TRANS_REFUND.name())) {
            // 平安信用支付买家会员间交易到卖家退款
            // 1879737844562415675_PINGAN_CREDIT_PAY_ACCT_TRANS_REFUND
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.PINGAN_CREDIT_PAY_ACCT_TRANS_REFUND.name()) - 1));
        } else if (bizOrderNo.contains(AmountTypeEnum.XYD_CREDIT_PAY_ACCT_TRANS_REFUND.name())) {
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.XYD_CREDIT_PAY_ACCT_TRANS_REFUND.name()) - 1));
        } else {
            separateOrder.setBizNo(bizOrderNo);
        }
    }

    /**
     * 转换
     */
    public SeparateOrder transferSeparateOrder() {
        SeparateOrder separateOrder = new SeparateOrder();
        separateOrder.setCustNoIn(origInAccountId);
        separateOrder.setCustMerchant(origOutAccountId);
        separateOrder.setTradeAmount(amount);
        separateOrder.setBusinessOrderNo(businessRefundNo);
        fillSeparateOrderBizNo(separateOrder);
        separateOrder.setTradeCode(FbankTradeCodeEnum.FTC_11.getCode());
        separateOrder.setExtra("会员间交易退款");
        separateOrder.setStatus(SeparateOrder.STATUS_CREATE);
        separateOrder.setSettleNo(StringUtils.EMPTY);
        separateOrder.setCtime(DateUtil.now());
        separateOrder.setUtime(DateUtil.now());
        return separateOrder;
    }
}
